package controllers

import (
	"newAi/new_web/models"
	"strings"
)

type ServersController struct {
	BaseController
}

func (self *ServersController) List() {
	self.Data["pageTitle"] = "多机ip"
	self.display()
}



func (self *ServersController) Table() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	result, count := models.ServersGetList(page, self.pageSize, "servers", filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["servers"] = v.Serverip
		row["desc"] = v.Descs

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}



func (self *ServersController) Add() {
	//if self.isOrg == true {
	//	self.SetUserGroup(self.userId)
	//} else {
	//	self.SetAllGroup()
	//}
	//isCopy,_:=self.GetBool("isCopy")
	//if  isCopy {
	//	self.Data["isCopy"] = true
	//	self.Data["copyId"],_ = self.GetInt64("copyId")
	//}
	self.Data["pageTitle"] = "添加ip"
	self.display()
}

func (self *ServersController) Edit() {
	self.Data["pageTitle"] = "编辑ip"
	//if self.isOrg == true {
	//	self.SetUserGroup(self.userId)
	//} else {
	//	self.SetAllGroup()
	//}
	id, _ := self.GetInt("id", 0)
	tpl, _ := models.ServersGetById(id)
	row := make(map[string]interface{})
	row["id"] = tpl.Id
	row["servers"] = tpl.Serverip
	row["desc"] = tpl.Descs

	self.Data["server"] = row
	self.display()
}

func (self *ServersController) AjaxSave() {
	servers := new(models.AiServers)

	servers.Serverip = strings.TrimSpace(self.GetString("ip"))
	servers.Descs = strings.TrimSpace(self.GetString("desc"))
	//判断 是否存在 ID， 如果有ID的话，就进行update
	if i, _ := self.GetInt64("id"); i != 0 {
		servers.Id = i
		if err := servers.Update(); err != nil {
			self.ajaxMsg(err.Error(), MSG_ERR)
		}
		self.ajaxMsg("", MSG_OK)
	}
	//如果没有ID  ，就进行增加的
	_, err := models.ServersAdd(servers)
	if err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
		return
	}


	self.ajaxMsg("", MSG_OK)
}

func (self *ServersController) AjaxDel() {
	id, _ := self.GetInt("id")
	server, _ := models.ServersGetById(id)

	if err := server.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}

	self.ajaxMsg("", MSG_OK)
}




